﻿using System.Collections.ObjectModel;
using Catel.Data;
using Catel.MVVM;

namespace Catel.Articles._03___MVVM.Examples.NestedUserControls
{
    /// <summary>
    /// Table view model.
    /// </summary>
    public class TableViewModel : ViewModelBase
    {
        #region Variables
        private int _chairIndex = 1;
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="TableViewModel"/> class.
        /// </summary>
        /// <param name="table">The table.</param>
        public TableViewModel(Models.Table table)
        {
            // Store values
            Table = table;

            // Create commands
            AddChair = new Command<object>(AddChair_Execute);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the title of the view model.
        /// </summary>
        /// <value>The title.</value>
        public override string Title { get { return "Table"; } }

        #region Models
        /// <summary>
        /// Gets or sets the table.
        /// </summary>
        [Model]
        public Models.Table Table
        {
            get { return GetValue<Models.Table>(TableProperty); }
            private set { SetValue(TableProperty, value); }
        }

        /// <summary>
        /// Register the Table property so it is known in the class.
        /// </summary>
        public static readonly PropertyData TableProperty = RegisterProperty("Table", typeof(Models.Table));
        #endregion

        #region View model
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [ViewModelToModel("Table")]
        public string Name
        {
            get { return GetValue<string>(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        /// <summary>
        /// Register the Name property so it is known in the class.
        /// </summary>
        public static readonly PropertyData NameProperty = RegisterProperty("Name", typeof(string));

        /// <summary>
        /// Gets or sets the chair collection.
        /// </summary>
        [ViewModelToModel("Table")]
        public ObservableCollection<Models.Chair> Chairs
        {
            get { return GetValue<ObservableCollection<Models.Chair>>(ChairsProperty); }
            set { SetValue(ChairsProperty, value); }
        }

        /// <summary>
        /// Register the Chairs property so it is known in the class.
        /// </summary>
        public static readonly PropertyData ChairsProperty = RegisterProperty("Chairs", typeof(ObservableCollection<Models.Chair>));
        #endregion
        #endregion

        #region Commands
        /// <summary>
        /// Gets the AddChair command.
        /// </summary>
        public Command<object> AddChair { get; private set; }

        /// <summary>
        /// Method to invoke when the AddChair command is executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private void AddChair_Execute(object parameter)
        {
            Chairs.Add(new Models.Chair(string.Format("Chair {0}", _chairIndex++)));
        }
        #endregion
    }
}
